#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>

void handle_sig(int sig)
{
    printf("sinal pipe %d\n",sig);
    exit(1);
}
int main(int argc, char **argv)
{
    int fd;
    int ret;
    char buff[128];
    int i = 0;

    signal(SIGPIPE,handle_sig);
    if(mkfifo("./fifo",0640) == -1 )
    {
        if(errno != EEXIST)
        {
            perror("mkfifo");
            exit(1);
        }
    }

    fd = open("./fifo",O_WRONLY);
    if(fd == -1)
    {
        perror("open");
        exit(1);
    }
    //write data
    while(1)
    {
        sprintf(buff,"data %d",i++);
        ret = write(fd,buff,strlen(buff));
        printf("write fifo %d %s\n",ret,buff);
        sleep(1);
    }
    return 0;
}
